var schoolID;
var standardSelected;
var standardSelectedAddSubject;
var divisionSelectedAddSubject;

$(document).ready(function (){
	//cookie values
	var cookieValue=$.cookie("school");
	var cookieValueArray = cookieValue.split('/');
	schoolID = cookieValueArray[0];
});

function examConf(){	
	getExamConf();
	getStandardList();
}

function getExamConf(){	
	var url="php/getExamConf.php";
		
	$.ajax({
	url: url,
	type: 'GET',
	dataType: "json",
	data: 'school_id='+schoolID,
	success: function(data) {
		if(data.error=="false")
		{
			$('#testExamConf').val(data.list.test);
			$('#mainExamConf').val(data.list.main);
		}
	},
	error: function(e) {
		console.debug("error");
	}
	});
}

function updateExamConf()
{
	var test = $('#testExamConf').val();
	var main = $('#mainExamConf').val();
	
	if (test == "" || main == "") 
		return 
		
	var data = {
		"test": test, 
		"main": main,
		"school_id": schoolID
	}; 
	
	var url;
	if(document.domain=="localhost")
	{
		url="http://localhost/lifeinclass/php/updateExamConfig.php";
	}else{
		url="php/updateExamConfig.php";
	}
	
	$.ajax({
	type: "POST",
	dataType: "json",
	url: url, 
	data: data,
	success: function(data) {
		if(data.error == "false"){			
			
		}
	},
	error: function(e) {
		console.debug(e);
	}
	}); 
}

function addStandard()
{
	var name = $('#standardName').val();
	
	if (name == "") 
		return 
		
	var data = {
		"name": name,
		"school_id": schoolID
	}; 
	
	var url;
	if(document.domain=="localhost")
	{
		url="http://localhost/lifeinclass/php/addStandard.php";
	}else{
		url="php/addStandard.php";
	}
	
	$.ajax({
	type: "POST",
	dataType: "json",
	url: url, 
	data: data,
	success: function(data) {
		if(!data.error){			
			$('#standardName').val('');
			getStandardList();
		}
	},
	error: function(e) {
		console.debug(e);
	}
	}); 
}

function getStandardList(){
	var url="php/getStandard.php";
		 
	$.ajax({
	url: url,
	type: 'GET',
	dataType: "json",
	data: 'school_id='+schoolID,
	success: function(data) {
		if(data.error=="false")
		{
			for(var x=0;x<(data.list.length);x++)
			{				 
				 $("#student_adddivision_config").append("<li><a onclick=\"examConfStandardOnClick(this)\" value=\""+data.list[x].id+"\">"+data.list[x].name+"</a></li>");
				 
				 $("#student_addsubject_standard_configSubject").append("<li><a onclick=\"examConfStandardAddSubjectOnClick(this)\" value=\""+data.list[x].id+"\">"+data.list[x].name+"</a></li>");
			}		
		}
	},
	error: function(e) {
		console.debug("error");
	}
	});
}

function examConfStandardOnClick(anchor) {
    standardSelected = anchor.getAttribute('value'); 
}

function examConfStandardAddSubjectOnClick(anchor) {
	standardSelectedAddSubject = "";
	divisionSelectedAddSubject = "";
	
    standardSelectedAddSubject = anchor.getAttribute('value'); 
    
	$('#student_addsubject_division_configSubject').empty();
	
    var url="php/getDivision.php"; 
    
	$.ajax({
	url: url,
	type: 'GET',
	dataType: "json",
	data: 'standard_id='+standardSelectedAddSubject,
	success: function(data) {
		if(data.error=="false")
		{
			for(var x=0;x<(data.list.length);x++)
			{				 
				 $("#student_addsubject_division_configSubject").append("<li><a onclick=\"examConfDivisionOnClick(this)\" value=\""+data.list[x].id+"\">"+data.list[x].name+"</a></li>");
			}		
		}
	},
	error: function(e) {
		console.debug("error");
	}
	});
}

function examConfDivisionOnClick(anchor) {
	divisionSelectedAddSubject = anchor.getAttribute('value');
}

function addSubject(){	
	var name = $('#addSubjectName').val();
	var test_max = $('#addSubjectTestMax').val();
	var main_max = $('#addSubjectMainMax').val();
	
	if (name == "" || test_max == "" || main_max == "" || standardSelectedAddSubject == "" || typeof standardSelectedAddSubject === "undefined"|| divisionSelectedAddSubject == "" || typeof divisionSelectedAddSubject === "undefined" ) 
		return 
		
	var data = {
		"name": name,
		"test_max": test_max,
		"main_max": main_max,
		"standard_id": standardSelectedAddSubject,
		"division_id": divisionSelectedAddSubject,
		"school_id": schoolID
	}; 
	
	var url;
	if(document.domain=="localhost")
	{
		url="http://localhost/lifeinclass/php/addSubject.php";
	}else{
		url="php/addSubject.php";
	}
	
	$.ajax({
	type: "POST",
	dataType: "json",
	url: url, 
	data: data,
	success: function(data) {
		if(!data.error){			
			$('#addSubjectName').val('');
			$('#addSubjectTestMax').val('');
			$('#addSubjectMainMax').val('');
		}
	},
	error: function(e) {
		console.debug(e);
	}
	}); 
}

function addDivision(){
		
	var name = $('#ExamDivision').val();
	
	if (name == "" || standardSelected == "" || typeof standardSelected === "undefined" ) 
		return 
		
	var data = {
		"name": name,
		"standard_id": standardSelected
	}; 
	
	var url;
	if(document.domain=="localhost")
	{
		url="http://localhost/lifeinclass/php/addDivision.php";
	}else{
		url="php/addDivision.php";
	}
	
	$.ajax({
	type: "POST",
	dataType: "json",
	url: url, 
	data: data,
	success: function(data) {
		if(!data.error){			
			$('#ExamDivision').val('');
			//getStandardList();
		}
	},
	error: function(e) {
		console.debug(e);
	}
	}); 
}
